package com.yupi.project.handler;

import com.yupi.project.model.AuthenticatorResult;
import com.yupi.project.model.ProtectedRequest;
import com.yupi.project.security.SecurityVerificationHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
 * @description: 日志记录
 * @author SummerBird
 * @date 2024/3/8 19:43
 * @version 1.0
 */
@Component
@Slf4j
public class LogHandler implements SecurityVerificationHandler {

    @Override
    public AuthenticatorResult auth(ProtectedRequest protectedRequest) {
        ServerHttpRequest request = protectedRequest.getRequest();
        HttpHeaders headers = request.getHeaders();
        String id = request.getId();
        String path = request.getURI().toString();
        InetSocketAddress remoteAddress = request.getRemoteAddress();
        InetSocketAddress localAddress = request.getLocalAddress();
        String method = request.getMethod().name();
        log.info("请求头是：" + headers);
        log.info("发送id是：" + id);
        log.info("请求路径是:" + path);
        log.info("请求IP源地址是：" + remoteAddress.getAddress());
        log.info("请求IP目标地址是：" + localAddress.getHostString());
        log.info("请求方法是：" + method);
        return AuthenticatorResult.success();
    }

    @Override
    public int getOrder() {
        return 50;
    }
}
